package cn.com.eado.crm.v.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import cn.com.eado.preado.app.utils.autoCleared

open class BaseBindingFragment<T : ViewDataBinding>(private val layoutId: Int) : Fragment() {

    private var isViewInit = false//记录是否已经初始化过一次视图
    private var lastView: View? = null//记录上次创建的view

    var binding by autoCleared<T>()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (lastView == null){
            binding = DataBindingUtil.inflate(inflater, layoutId, container, false)
            binding.lifecycleOwner = this
            lastView = binding.root
        }
        return lastView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        if (!isViewInit){
            super.onViewCreated(view, savedInstanceState)
            initTheFragment()
            isViewInit = true
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
    }

    open fun initTheFragment(){}

    open fun togoGongDanFragment(){}
}